package manage.action;

import m.common.action.ActionMeta;
import m.common.action.RequestMeta;
import m.common.model.util.ModelQueryList;
import m.common.model.util.QueryCondition;
import m.common.model.util.QueryOrder;
import m.system.util.JSONMessage;
import m.system.util.StringUtil;
import manage.model.AdminLogin;
import manage.model.FileInfo;
import manage.model.ImageAdmin;
import manage.service.FileInfoService;
import manage.service.ImageAdminService;

import java.io.File;
import java.util.Map;

@ActionMeta(name="manage/fileInfo")
public class FileInfoAction extends ManageAction {
	private String adminToken;
	private String path;
	private String fileType;
	private String businessOid;
	
	/**
	 * 上传文件
	 * 
	 */
	public JSONMessage upload() throws Exception {
		JSONMessage message=new JSONMessage(0);
		FileInfo model=new FileInfo();
		ImageAdmin ia=new ImageAdmin();
		if(StringUtil.isSpace(adminToken)||StringUtil.noSpace(adminToken).length()<3){
			AdminLogin admin=verifyAdminLogin();
			ia.setOid(admin.getOid());
		}else{
			ia.setOid(getService(ImageAdminService.class).getOid(adminToken));
		}
		Map<String,File> map=super.getFileMap();
		for(String key : map.keySet()){
			String name=map.get(key).getName();
			model.setOid("");
			model.setPath(path);
			model.setFileType(fileType);
			model.setBusinessOid(businessOid);
			model.setName(name.substring(name.indexOf("_")+1));
			model.setImageAdmin(ia);
			getService(FileInfoService.class).saveFile(model, map.get(key));
			message.push("model", model);
			break;
		}
		return message;
	}
	public JSONMessage getBusiFile() throws Exception {
		ImageAdmin ia=new ImageAdmin();
		if(StringUtil.isSpace(adminToken)||StringUtil.noSpace(adminToken).length()<3){
			AdminLogin admin=verifyAdminLogin();
			ia.setOid(admin.getOid());
		}else{
			ia.setOid(getService(ImageAdminService.class).getOid(adminToken));
		}
		return new JSONMessage(0)
			.push("list", ModelQueryList.getModelList(FileInfo.class, new String[] {"*"}, null,
				QueryCondition.and(
					QueryCondition.eq("businessOid", businessOid),
					QueryCondition.eq("fileType", fileType)
				),
				QueryOrder.asc("createDate")))
			.push("admin", ia);
		
	}
@RequestMeta(params= {"fileOid"})
	public JSONMessage delete(String fileOid) throws Exception {
		ImageAdmin ia=new ImageAdmin();
		if(StringUtil.isSpace(adminToken)||StringUtil.noSpace(adminToken).length()<3){
			AdminLogin admin=verifyAdminLogin();
			ia.setOid(admin.getOid());
		}else{
			ia.setOid(getService(ImageAdminService.class).getOid(adminToken));
		}
		return new JSONMessage(0,"删除成功")
				.push("oid", getService(FileInfoService.class).delete(ia, fileOid));
	}

	public String getBusinessOid() {
		return businessOid;
	}

	public void setBusinessOid(String businessOid) {
		this.businessOid = businessOid;
	}

	public String getFileType() {
		return fileType;
	}

	public void setFileType(String fileType) {
		this.fileType = fileType;
	}

	public String getAdminToken() {
		return adminToken;
	}

	public void setAdminToken(String adminToken) {
		this.adminToken = adminToken;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

}
